package com.kcnet.qk.controller.pyq;

import android.view.accessibility.AccessibilityEvent;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import cn.jesse.nativelogger.NLogger;

/**
 * Created by Administrator on 2018/4/25 0025.
 * 发送朋友圈视频
 * 视频长度不能超过10秒
 */

public class PyqSendFriendVideo extends BasePeopleController {

    private static String TAG = "发送朋友圈视频";

    private String SendText = "测试视频文本"; //发送的文本

    private int static_c = 0;//执行步骤

    private static PyqSendFriendVideo mRongCloudInstance;



    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static PyqSendFriendVideo getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (PyqSendFriendVideo.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new PyqSendFriendVideo();
                }
            }
        }

        return mRongCloudInstance;
    }


    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();

        if ( mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.ui.LauncherUI")) {
            NLogger.d(TAG, "1、进入微信主页，进入朋友圈......");
            RootShell.execShellCmd("am start -n com.tencent.mm/com.tencent.mm.plugin.sns.ui.SnsTimeLineUI");
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.plugin.sns.ui.SnsTimeLineUI")) {
            NLogger.d(TAG, "2、到达朋友圈主页面，点击拍照分享");
            tagIdClick("com.tencent.mm:id/hh");
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.ui.base.k")) {
            NLogger.d(TAG, "3、发布朋友圈弹窗，点击弹出框的从相册选择");
            tagTextClick("从相册选择");
            static_c = 4;//这一段会引起重复点击，所以用步骤来限制
        } else if (static_c == 4 && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.plugin.gallery.ui.AlbumPreviewUI")) {
            NLogger.d(TAG, "4、到达相册，点击选择视频");
            tagIdClick("com.tencent.mm:id/d5r");
            static_c = 5;//执行过点击就不再执行了
        } else if (static_c == 5 && mType == AccessibilityEvent.TYPE_VIEW_CLICKED && mClassName.equals("android.widget.LinearLayout")) {
            NLogger.d(TAG, "5、点击 所有视频");
//            tagIdClick("com.tencent.mm:id/d5x");
            tagTextClick("所有视频");
            static_c = 6;
        } else if (mType == AccessibilityEvent.TYPE_VIEW_FOCUSED && mClassName.equals("android.widget.GridView")) {
            NLogger.d(TAG, "6、进入视频界面列表，点击预览，（默认进入点击第一个视频）");
            tagIdClick("com.tencent.mm:id/cqe");
        }else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.plugin.gallery.ui.ImagePreviewUI")) {
            NLogger.d(TAG, "7、到达视频预览界面，点击 完成");
            tagIdClick("com.tencent.mm:id/hg");
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.tencent.mm.plugin.sns.ui.SnsUploadUI")) {
            NLogger.d(TAG, "8、到达发表界面");
            RootShell.click(100,115);
            sleepTime(1000);
            inputSayHiSentence(SendText);
            NLogger.d(TAG, "9、点击发表");
            sleepTime(1000);
            RootShell.click(450,38);
            stopTask();
        }
        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.DialogUI)) {
            if(!tagTextClick("确定")){
                tagTextClick("我知道了");
            }
        }
    }

    public void setParms(int maxAddCount, int minAddTime, int maxAddTime,String text) {
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.SendText =  text;
    }


}
